package com.javasm.crm.service.aftersale.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.entity.aftersale.Cities;
import com.javasm.crm.domin.vo.aftersale.CitiesVO;
import com.javasm.crm.mapper.aftersale.CitiesMapper;
import com.javasm.crm.service.aftersale.ICitiesService;
import com.javasm.crm.utils.transfer.CitiesTransfer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.PriorityQueue;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lirui
 * @since 2021-07-17
 */
@Service
public class CitiesServiceImpl extends ServiceImpl<CitiesMapper, Cities> implements ICitiesService {

    @Resource
    private CitiesTransfer citiesTransfer;

    @Override
    public List<CitiesVO> getCities(CitiesVO citiesVO) {

        List<CitiesVO> parent = citiesVO == null ? getParent(0L) : getParent(citiesVO.getId());

        if (parent != null && parent.size() > 0) {
            for (CitiesVO vo : parent) {
                if (vo != null) {
                    vo.setChildren(getCities(vo));
                }
            }
        }

        return parent;
    }

    public List<CitiesVO> getParent(Long id) {

        QueryWrapper<Cities> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Cities> lambda = queryWrapper.lambda();

        lambda.eq(Cities::getParentId, id);
        List<Cities> list = list(queryWrapper);

        return citiesTransfer.toVO(list);
    }
}
